package com.zdd.ehe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdd.ehe.entity.NewsData;
import com.zdd.ehe.entity.PriceData;
import com.zdd.ehe.entity.Role;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.INewsDataService;
import com.zdd.ehe.service.impl.NewsDataServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhuiZhu
 * @since 2023-03-24
 */
@Api(tags="新闻管理模块")
@RestController
@RequestMapping("/newsData")
public class NewsDataController {

    @Resource
    private INewsDataService newsDataService;

    @ApiOperation("分页获取新闻接口")
    @PostMapping("/getallnewsbypage")
    public Result getAllNewsByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        Page<NewsData> pagex = new Page<>(page,pageSize);
        Page<NewsData> pageResult = newsDataService.getAllNewsByPage(pagex);
        if(pageResult.getSize() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",pageResult);
    }

    @ApiOperation("根据新闻标题title查询")
    @GetMapping("/selectbytitle")
    public Result selectByTitle(@RequestParam("title") String title){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("title",title);
        List<NewsData> newsDataList = newsDataService.list(queryWrapper);
        if(newsDataList.size() !=0){
            Map<String,Object> data = new HashMap<>();
            data.put("newsDataList",newsDataList);
            data.put("total",newsDataList.size());
            data.put("size",10);
            data.put("current",1);
            data.put("pages",1);
            return Result.success("查询成功!",data);
        }else{
            return Result.error(4001,"未查询到该新闻");
        }

    }


    @ApiOperation("新增新闻数据接口")
    @PostMapping("/addnewsdata")
    public Result addNewsData(@RequestBody NewsData newsData){
        int r = newsDataService.addNewsData(newsData);
        if(r == 1){
            return Result.success("添加成功");
        }else{
            return Result.error(4001,"添加失败");
        }
    }


    @ApiOperation("删除接口")
    @PostMapping("/deletenewsdatabyid")
    public Result deleteNewsDataById(@RequestParam("id") int id){
        NewsData newsData = newsDataService.getById(id);
        if(newsData == null){
            return Result.error(4001,"编号错误");
        }
        newsData.setStatus("0");
        int result = newsDataService.updateNewsDataById(newsData);
        if(result == -1){
            return Result.error(4002,"删除失败");
        }
        return Result.success("删除成功");
    }


    @ApiOperation("重启接口")
    @PostMapping("/restartnewsdatabyid")
    public Result restartNewsDataById(@RequestParam("id") int id){
        NewsData newsData = newsDataService.getById(id);
        if(newsData == null){
            return Result.error(4001,"编号错误");
        }
        newsData.setStatus("1");
        int result = newsDataService.updateNewsDataById(newsData);
        if(result == -1){
            return Result.error(4002,"重启失败");
        }
        return Result.success("重启成功");
    }


    @ApiOperation("修改新闻数据接口")
    @PostMapping("/updatenewsdata")
    public Result updateNewsData(@RequestBody NewsData newsData){
        int result = newsDataService.updateNewsDataById(newsData);
        if(result == -1){
            return Result.error("修改失败");
        }
        return Result.success("修改成功");
    }


}
